#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>

#define MAXLINE 1024
#define MAXPARAM 32
#define MIN
/*
    30m-(1-10?)m-20m
    60-(2-20?)-40
*/
int positive_1 = 0;
int positive_2 = 0;
int positive_3 = 0;
int negetive = 0;
char in_name[128] = "dynamic/40-1-20-bt.csv";
char out_name[128] = "dynamic_normalization/1-bt.csv";

int analyse_label(int label){
    if (label==-1) {
        return label;
    }
    int ioh = label % 10;
    int front = label / 1000;
    int back = (label - (front * 1000)) / 10;
    if (front <= 10 && front>0) {
        positive_1++;
        return 1;
    }else if (front <= 20 && front>0) {
        positive_2++;
        return 2;
    }else if (front <= 30 && front>0) {
        positive_3++;
        return 3;
    }
    if (ioh == 1) {
        return -2;
    }
    if ((front==0 || front>60) && (back==0 || back>40)) {
        if (rand()%100 <= 12) {
            negetive++;
            return 0;
        }else {
            return -10;
        }
    }
    return -3;
}

void traverse(char *in_path, char *out_path){
    FILE *in = fopen(in_path, "r+");
    FILE *out = fopen(out_path, "w+");
    
    char line_buffer[MAXLINE];
    fgets(line_buffer, MAXLINE, in);
    fprintf(out, "label,label1,caseid,PATIENT_ID,VISIT_ID,OPER_ID,ITEM_NO,MONITOR_TIME,MAP,SDP,HR,BT\n");
    int i = 0;
    while (fgets(line_buffer, MAXLINE, in)) {
        int label;
        #ifdef MIN
        if (i%2==0) {
        #endif
            sscanf(line_buffer, "%i", &label);
            label = analyse_label(label);
            fprintf(out, "%i,%s", label, line_buffer);
        #ifdef MIN
        }
        #endif
        i++;
    }
   // printf("line: %i", i);
    fclose(in);
    fclose(out);
}

int main(int argc, char *argv[]){
    srand(time(NULL));
    traverse(in_name, out_name);
    printf("%i, %i, %i, %i\n", positive_1, positive_2, positive_3, negetive);
    return 0;
}
